/*-------------------------------------------------
	File		: MainCanvas.java
	Author		: Andik Taufiq
	DateTime	: 2010/09/18 10:28
---------------------------------------------------*/

import javax.microedition.lcdui.*;
import java.io.IOException;

class MainCanvas extends Canvas implements Runnable, CommandListener {
	public static Display display;
	public static PageTransition3 midlet;

	private Image background;
	private Image header;
	private Image footer;

	public static Font plainFont;
    public static Font boldFont;
	public static Font underlinedFont;
	public static Font italicFont;
	public static Font mediumPlainFont;
    public static Font mediumBoldFont;
	public static Font mediumUnderlinedFont;
	public static Font mediumItalicFont;

	private int index = 1;

	private int xDrawingArea = 5;
	private int yDrawingArea = 40;
	private int widthDrawingArea = 225;

	private int page = 1;

	private int move = 0; // penanda pergerakan, 1 jika ke kanan, -1 jika ke kiri, 0 jika diam
	private boolean allowToPress = true; // kondisi apakah boleh melakukan aksi keyPressed
	private Image img; // Image penyimpan tampilan secara off-screen
	private Graphics graph; // graphics penggambar tampilan off-screen
	private Image transparan; // gambar untuk menutupi bagian yang tidak termasking

	// Loading
    private boolean showLoadingPage = false;
    private int loadingIndex = 1;
    private static int nbBar = 7;
    private long endLoading;
    private long intervalLoading = 2500;

	private CustomPage cp;
	private CustomScrollBar cs;
	private CustomForm cf;
	private CustomComponent cc1;
	private CustomComponent cc2;
	private CustomComponent cc3;
	private CustomComponent cc4;
	private CustomComponent cc5;
	private CustomComponent cc5b;
	private CustomComponent cc5c;
	private CustomComponent cc5d;
	private CustomComponent cc5e;
	private RadioButton rb1;
	private RadioButton rb2;
	private RadioButton rb3;
	private CustomPage cp2;
	private CustomScrollBar cs2;
	private CustomForm cf2;
	private CustomComponent cc6;
	private CustomComponent cc7;
	private CustomComponent cc8;
	private CustomPage cp3;
	private CustomScrollBar cs3;
	private CustomForm cf3;
	private CustomComponent cc9;
	private CustomComponent cc10;
	private CustomComponent cc10b;
	private CustomComponent cc10c;
	private CustomComponent cc10d;
	private CustomComponent cc10e;
	private RadioButton rb4;
	private RadioButton rb5;
	private RadioButton rb6;
	private RadioButton rb7;
	private RadioButton rb8;
	private RadioButton rb9;

	public MainCanvas(PageTransition3 midlet, Display display) {
		setFullScreenMode(true);	// set agar canvas ditampilkan secara fullscreen
		this.midlet = midlet;
		this.display = display;
		plainFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        boldFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
		underlinedFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_SMALL);
		italicFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_SMALL);
		mediumPlainFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        mediumBoldFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
		mediumUnderlinedFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM);
		mediumItalicFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_MEDIUM);
		try {
            header = Image.createImage("/HeaderComponent.png");
            footer = Image.createImage("/FooterComponent.png");
            background = Image.createImage("/Background.jpg");
			img = Image.createImage(getWidth(), getHeight());
			transparan = Image.createImage("/Transparan.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		cp = new CustomPage("Page 1", 0, 40, 240, 1);
		cp.setPadding(5, 0, 15, 0);
		cs = new CustomScrollBar(40, cp.windowHeight, 5);
		cf = new CustomForm("my form");
		cc1 = new CustomComponent("Bersedia mengisi check list ini", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_CHECK_BOX);
		cc1.setIndex(1);
		cc2 = new CustomComponent("Agama", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_COMBO_BOX);
		cc2.add("Islam");
		cc2.add("Katholik");
		cc2.add("Protestan");
		cc2.add("Hindu");
		cc2.add("Budha");
		cc3 = new CustomComponent("Nama Lengkap", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc4 = new CustomComponent("Setuju dengan pernyataan ini:", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_RADIO);
		rb1 = new RadioButton("Setuju", true, 0x0000FF, 0xFFFFFF, true);
		rb2 = new RadioButton("Ragu-ragu", false, 0x0000FF, 0xFFFFFF, true);
		rb3 = new RadioButton("Tidak Setuju", false, 0x0000FF, 0xFFFFFF, true);
		cc5 = new CustomComponent("Alamat", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc5b = new CustomComponent("Alamat Libur", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc5c = new CustomComponent("Nomor Telepon / HP", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc5d = new CustomComponent("Kewarganegaraan", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc5e = new CustomComponent("Nomor Kartu Identitas (KTP / SIM)", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc4.add(rb1);
		cc4.add(rb2);
		cc4.add(rb3);
		cf.add(cc1);
		cf.add(cc2);
		cf.add(cc3);
		cf.add(cc4);
		cf.add(cc5);
		cf.add(cc5b);
		cf.add(cc5c);
		cf.add(cc5d);
		cf.add(cc5e);
		cp.add(cf);
		cp.add(cs);

		cp2 = new CustomPage("Page 2", 0, 40, 240, 1);
		cp2.setPadding(5, 0, 15, 0);
		cs2 = new CustomScrollBar(40, cp2.windowHeight, 5);
		cf2 = new CustomForm("my form");
		cc6 = new CustomComponent("Bidang Pekerjaan", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_COMBO_BOX);
		cc6.setIndex(1);
		cc6.add("Jasa");
		cc6.add("Teknologi Informasi");
		cc6.add("Wirausaha");
		cc6.add("Pegawai Negeri Sipil");
		cc7 = new CustomComponent("Gambaran Diri Pribadi", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc8 = new CustomComponent("Tidak pernah terlibat dalam kriminalitas", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_CHECK_BOX);
		cf2.add(cc6);
		cf2.add(cc7);
		cf2.add(cc8);
		cp2.add(cf2);
		cp2.add(cs2);

		cp3 = new CustomPage("Page 3", 0, 40, 240, 1);
		cp3.setPadding(5, 0, 15, 0);
		cs3 = new CustomScrollBar(40, cp3.windowHeight, 5);
		cf3 = new CustomForm("my form");
		cc9 = new CustomComponent("Warna favorit", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_RADIO);
		cc9.setIndex(1);
		rb4 = new RadioButton("Merah", false, 0x0000FF, 0xFFFFFF, true);
		rb5 = new RadioButton("Biru", false, 0x0000FF, 0xFFFFFF, true);
		rb6 = new RadioButton("Hijau", true, 0x0000FF, 0xFFFFFF, true);
		rb7 = new RadioButton("Kuning", false, 0x0000FF, 0xFFFFFF, true);
		rb8 = new RadioButton("Ungu", false, 0x0000FF, 0xFFFFFF, true);
		rb9 = new RadioButton("Hitam", false, 0x0000FF, 0xFFFFFF, true);
		cc9.add(rb4);
		cc9.add(rb5);
		cc9.add(rb6);
		cc9.add(rb7);
		cc9.add(rb8);
		cc9.add(rb9);
		cc10 = new CustomComponent("Pesan dan Kesan", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc10b = new CustomComponent("Hobi", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc10c = new CustomComponent("Alasan mengisi form ini", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc10d = new CustomComponent("Harapan ke depan", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cc10e = new CustomComponent("Gaji yang diinginkan", 0x222222, 0xFFFFFF, true, CustomComponent.TYPE_INPUT_TEXT);
		cf3.add(cc9);
		cf3.add(cc10);
		cf3.add(cc10b);
		cf3.add(cc10c);
		cf3.add(cc10d);
		cf3.add(cc10e);
		cp3.add(cf3);
		cp3.add(cs3);
		graph = img.getGraphics();
		paintOffScreen(graph);
	}

	private void drawBackground(Graphics g, Image img) {
		if (img != null) {
			g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT);
		}
	}

	private void drawHeader(Graphics g, Image img, String title) {
		if (img != null) {
			g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT);
			if (title != null) {
				g.setColor(0xFFFFFF);
				g.setFont(mediumBoldFont);
				g.drawString(title,getWidth()/2,5,Graphics.TOP|Graphics.HCENTER);
			}
		}
	}

	private void drawFooter(Graphics g, Image img, String leftMenu, String rightMenu) {
		if (img != null) {
			g.drawImage(img, 0, getHeight(), Graphics.BOTTOM | Graphics.LEFT);
			g.setColor(0xFFFFFF);
			g.setFont(boldFont);
			g.drawString(leftMenu,6,getHeight()-4,Graphics.BOTTOM|Graphics.LEFT);
			g.drawString(rightMenu,getWidth()-6,getHeight()-4,Graphics.BOTTOM|Graphics.RIGHT);
		}
	}

	protected void paint(Graphics g) {
		g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
		if (allowToPress == false) {
			if (showLoadingPage == true) {
				drawLoadingPage(g, "memuat data...", loadingIndex, 0xCCCCCC, 0x555555,
						0xA849A3, 0x111111, 2, 100, false);
			}
		}
		display.callSerially(this);
	}

	protected void keyPressed(int keyCode) {
		switch (page) {
			case 1:
				cp.keyPressed(keyCode);
				paintOffScreen(graph);
				break;
			case 2:
				cp2.keyPressed(keyCode);
				paintOffScreen(graph);
				break;
			case 3:
				cp3.keyPressed(keyCode);
				paintOffScreen(graph);
				break;
		}
		switch (keyCode) {
			case -3:
				if (page > 1) {
					if (allowToPress == true) {
						cp.currY = cp.y;
						cp2.currY = cp2.y;
						cp3.currY = cp3.y;
						move = 1;
						allowToPress = false;
						showLoadingPage = true;
						endLoading = System.currentTimeMillis() + intervalLoading;
					}
				}
				break;
			case -4:
				if (page < 3) {
					if (allowToPress == true) {
						cp.currY = cp.y;
						cp2.currY = cp2.y;
						cp3.currY = cp3.y;
						move = -1;
						allowToPress = false;
						showLoadingPage = true;
						endLoading = System.currentTimeMillis() + intervalLoading;
					}
				}
				break;
			case -7:
				midlet.exitMIDlet();
				break;
		}
		repaint();
	}

	public void run() {
		if (allowToPress == false) {
			if (showLoadingPage == true) {
				if (System.currentTimeMillis() < endLoading ) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException ex) {
						ex.printStackTrace();
					}
					if (loadingIndex < 7) {
						loadingIndex++;
					} else {
						loadingIndex = 1;
					}
				} else {
					if (move == 1){
						page--;
						move = 0;
					} else if (move == -1) {
						page++;
						move = 0;
					}
					allowToPress = true;
					showLoadingPage = false;
				}
			}
			paintOffScreen(graph);
			repaint();
		}
	}

	private void paintOffScreen(Graphics g) {
		drawBackground(g, background);
		switch (page) {
			case 1:
				cp.paint(g);
				drawHeader(g, header, "Page Transition 3 - " + cp.name);
				break;
			case 2:
				cp2.paint(g);
				drawHeader(g, header, "Page Transition 3 - " + cp2.name);
				break;
			case 3:
				cp3.paint(g);
				drawHeader(g, header, "Page Transition 3 - " + cp3.name);
				break;
		}
		drawFooter(g, footer, "Submit", "Exit");
	}

	public void commandAction(Command c, Displayable d) {
		cp.commandAction(c, d);
		cp2.commandAction(c, d);
		cp3.commandAction(c, d);
	}

	/*
     * Method untuk menggambarkan halaman loading
     * */
    private void drawLoadingPage(Graphics g, String msg, int index,
            int textColor, int barColor, int activeColor, int backColor,
            int pos, int height, boolean fullScreen) {
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 16; j++){
                g.drawImage(transparan, i*20, j*20, Graphics.LEFT | Graphics.TOP);
            }
        }
        int y = 0;
        if (fullScreen == false) {
            switch (pos) {
                case 1:     // di bagian atas
                    y = 0;
                    break;
                case 2:     // di bagian tengah
                    y = (getHeight() - height) / 2;
                    break;
                case 3:     // di bagian bawah
                    y = getHeight() - height;
                    break;
            }
        } else {
            height = getHeight();
        }
        g.setColor(backColor);
        g.fillRect(0, y, getWidth(), height);   // background
        int barHeight = 10;
        int barWidth = 20;
        int barInterval = 4;
        int barNumber = 7;
        int barY = y + (height / 2);
        int barX = (getWidth() / 2) - ((barNumber / 2) * (barWidth + barInterval))
                - (barInterval / 2) - ((barNumber % 2) * ((barWidth + barInterval) / 2));
        for (int i = 0; i < barNumber; i++) {
            if (i == index - 1) {
                g.setColor(activeColor);
            } else {
                g.setColor(barColor);
            }
            g.fillRect(barX, barY, barWidth, barHeight);
            barX = barX + barInterval + barWidth;
        }
        g.setColor(textColor);
        g.setFont(plainFont);
        g.drawString(msg, getWidth() / 2, barY - 4, Graphics.BOTTOM | Graphics.HCENTER);
    }
};